package service;

import java.sql.*;
import java.util.ArrayList;

import bean.Admin;
import db.DBcon; 

public class AdminService {
	private DBcon dbc=null;
	
	public AdminService()
	{
		dbc =new DBcon();
	}
	public ArrayList<Admin> queryAll() throws SQLException
	{
		ArrayList<Admin> results =new ArrayList<Admin>();
		String sql ="SELECT * FROM admin";
		ResultSet rs =dbc.doUqery(sql);
		while(rs.next())
		{
			Admin admin =new Admin();
			admin.setId(rs.getInt("Id"));
			admin.setUsername(rs.getString("username"));
			admin.setPassword(rs.getString("password"));
			results.add(admin);
		}
		return results;
	}
	public int delete(int id) throws SQLException
	{
		String sql="delete from admin where id ="+id;
		return dbc.doUpdate(sql);
	}
	public int add(Admin admin) throws SQLException
	{
		String sql="insert into admin (username,password) values('"+admin.getUsername()+"','"+admin.getPassword()+"')";
		return dbc.doUpdate(sql);
	}
	public Admin querybyid(int id) throws SQLException
	{
		String sql="select * from admin where id="+id;
		ResultSet rs =dbc.doUqery(sql);
		Admin admin= null;
		if(rs.next())
		{
			admin= new Admin();
			admin.setId(rs.getInt("id"));
			admin.setUsername(rs.getString("username"));
			admin.setPassword(rs.getString("password"));
		}
		return admin;
	}
	public int updatebyid(Admin admin) throws SQLException
	{
		String sql ="update admin set username='"+admin.getUsername()+"', password= '"+admin.getPassword()+"' where id='"+admin.getId()+"'";
		return dbc.doUpdate(sql);
	}
	public void close() throws SQLException
	{
		dbc.close();
	}
	
}
